package cn.com.zero.sample.trade.customer.service.impl;

import cn.com.zero.base.service.BaseService;
import cn.com.zero.db.annotation.DataSource;
import cn.com.zero.sample.trade.api.po.OrderPO;
import cn.com.zero.sample.trade.customer.mapper.IPaymentMapper;
import cn.com.zero.sample.trade.customer.mapper.IUserMapper;
import cn.com.zero.sample.trade.customer.po.User;
import cn.com.zero.sample.trade.customer.service.IPaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liuxh
 * @version 1.0.0
 * @Description 委托接口实现
 * @createTime 2022/7/27 17:08
 */
@Service
public class PaymentService extends BaseService implements IPaymentService {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IPaymentMapper paymentMapper;

    @Autowired
    private IUserMapper userMapper;



    public OrderPO getPaymentById(Long id) {
        return paymentMapper.getPaymentById(id);
    }

    public List<OrderPO> getPaymentList() {
        return paymentMapper.getPaymentList();
    }

    // 动态数据源事务，当不是默认数据源开启事务时，必须加@DataSource注解（方法或本类或调用该方法的类或方法都行，mapper不行），否则开启事务时会使用默认数据源。
    @DataSource("user")
    @Transactional(rollbackFor = Throwable.class)
    public void createUser(User user) throws Exception {
        int count = userMapper.create(user);
        if (count > 0) {
            log.info("用户添加成功。。。。");

            throw new Exception("异常：测试事务");
        }

    }


}
